package com.et.machine.store

import android.content.ContentProvider
import android.content.ContentUris
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.util.Log
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.BuildConfig

const val AUTHORITY = "com.et.core.provider"
const val GOODS_TABLE_NAME = "table_channel"
private const val TAG = "RoomProviderTest"

class RoomProvider : ContentProvider() {
    override fun onCreate(): Boolean {
        DataBaseUtil.init(context ?: return false)
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "query by provider")
            DataBaseUtil.channelDao(DataBaseUtil.IChannelDaoBox<IChannelDao>().apply {
                dao?.all()?.subscribe { it ->
                    it.forEach {
                        Log.d(TAG, it.toString())
                    }
                }
            })
        }
        val box = DataBaseUtil.IChannelDaoBox<IChannelDao>()
        val code = DataBaseUtil.channelDao(box)
        if (code != NORMAL) {
            ELog.d(TAGS.vending, "DataBase Not Ready [$code]")
            throw NullPointerException()
        }
        return box.dao?.findAll()
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        values?.let {
            Log.d(TAG, "insert by provider")
            val daoBox = DataBaseUtil.IChannelDaoBox<IChannelDao>()
            val code = DataBaseUtil.channelDao(daoBox)
            if (code != NORMAL) {
                ELog.d(TAGS.vending, "DataBase Not Ready [$code]")
                throw NullPointerException()
            }
            val box = it.getAsInteger("box")
            val channel = it.getAsInteger("channel")
            val label = it.getAsString("label")
            val price = it.getAsInteger("price")
            val status = it.getAsInteger("status")
            val quantity = it.getAsInteger("quantity")
            val barcode = it.getAsString("barcode")
            val name = it.getAsString("name")
            val image = it.getAsString("image_path_local")
            val discount = it.getAsInteger("discount")
            ChannelEntity.create(
                box = box, channel = channel, priceCent = price, barcode = barcode,
                discount = discount, status = when (status) {
                    0 -> ENABLED
                    1 -> DISABLED
                    2 -> LACK
                    else -> DISABLED
                }, quantity = quantity
            ).apply {
                this.label = label
                this.name = name
                this.image = image
                daoBox.dao?.insert(this)?.subscribe()
                context?.contentResolver
                    ?.notifyChange(uri, null)
                return ContentUris.withAppendedId(uri, this.uid.toLong())
            }
        }
        return uri
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
        Log.d(TAG, "delete by provider")
        val daoBox = DataBaseUtil.IChannelDaoBox<IChannelDao>()
        val code = DataBaseUtil.channelDao(daoBox)
        if (code != NORMAL) {
            ELog.d(TAGS.vending, "DataBase Not Ready [$code]")
            throw NullPointerException()
        }
        var count = 0
        if (context != null) {
            val last: String? = uri.lastPathSegment
            last?.let {
                count = daoBox.dao?.delete(it) ?: -1
                context!!.contentResolver
                    .notifyChange(uri, null)
            }
        }
        return count
    }

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        val daoBox = DataBaseUtil.IChannelDaoBox<IChannelDao>()
        val code = DataBaseUtil.channelDao(daoBox)
        if (code != NORMAL) {
            ELog.d(TAGS.vending, "DataBase Not Ready [$code]")
            throw NullPointerException()
        }
        var count = 0
        values?.let {
            val box = it.getAsInteger("box")
            val channel = it.getAsInteger("channel")
            val label = it.getAsString("label")
            val price = it.getAsInteger("price")
            val status = it.getAsInteger("status")
            val quantity = it.getAsInteger("quantity")
            val barcode = it.getAsString("barcode")
            val name = it.getAsString("name")
            val image = it.getAsString("image_path_local")
            val discount = it.getAsInteger("discount")
            ChannelEntity.create(
                box = box, channel = channel, priceCent = price, barcode = barcode,
                discount = discount, status = when (status) {
                    0 -> ENABLED
                    1 -> DISABLED
                    2 -> LACK
                    else -> DISABLED
                }, quantity = quantity
            ).apply {
                this.label = label
                this.name = name
                this.image = image
                count = daoBox.dao?.updateByProvider(this) ?: -1
                context?.contentResolver
                    ?.notifyChange(uri, null)

            }
        }
        return count
    }

    override fun getType(uri: Uri): String? {
        return null
    }
}